<template>
  <el-dialog :title="option.title" :visible.sync="option.isShow" width="70%" :close-on-click-modal="false"
    @open="openDialog" @close="closeDialog('ruleForm')">
    <el-row :gutter="10">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <!-- <el-col :span="12">
          <el-form-item label="ID" prop="ID">
            <el-input v-model="ruleForm.ID" size="small" disabled placeholder="请输入ID"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="渠道类型" prop="CHANNELTYPE">
            <el-select v-model="ruleForm.CHANNELTYPE" @change="changeItem" size="small" filterable clearable placeholder="请选择业务渠道">
              <el-option v-for="item in option.channelOption" :key="item.FIELDKEY" :label="item.FIELDVALUE"
                :value="item.FIELDKEY"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="业务类型" prop="BUSINESSTYPE">
            <el-select v-model="ruleForm.BUSINESSTYPE" :disabled="ruleForm.CHANNELTYPE == ''" @change="changeItem" size="small" filterable clearable placeholder="请选择业务类型">
              <el-option v-for="item in typeOption" :key="item.BUSINESSTYPE" :label="item.BUSINESSNAME"
                :value="item.BUSINESSTYPE"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交易码" prop="TRANSCODE">
            <el-select v-model="ruleForm.TRANSCODE" size="small"  filterable clearable placeholder="请输入交易码">
              <el-option v-for="item in relationOption" :key="item.TRANSCODE"
                :label="item.TRANSCODE+'—'+item.TRANSNAME" :value="item.TRANSCODE"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模板编号" prop="SCORETEMPLATEID">
            <el-select v-model="ruleForm.SCORETEMPLATEID" size="small" filterable clearable placeholder="请选择模板编号">
              <el-option v-for="item in option.SCORETEMPLATEIDOption" :key="item.SCORETEMPLATEID" :value="item.SCORETEMPLATEID" :label="item.SCORETEMPLATEID+'—'+item.SCORETEMPLATENAME"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        
        
        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" size="mini" @click="submitForm('ruleForm')">保存</el-button>
            <el-button @click="resetForm('ruleForm')" size="mini">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </el-dialog>
</template>

<script>
  import request from "@/common/utils/requestQC";
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import addData from "@/message/QCManage/scoreTemplate/templateChannelManage/templateChannelManageAdd";
  import updateData from "@/message/QCManage/scoreTemplate/templateChannelManage/templateChannelManageUpdate";
  import relation from "@/message/QCManage/scoreTemplate/templateChannelManage/templateRelation";
  import type from "@/message/QCManage/scoreTemplate/templateChannelManage/channelType";


  export default {
    props: {
      option: Object
    },
    computed: {
      isShow: {
        get: function () {
          return this.option.isShow;
        },
        set: function (newValue) {
          this.option.isShow = newValue;
        }
      }
    },
    mounted() {
      
    },
    data() {
      return {
        typeOption:[],
        relationOption:[],
        isInputOption:[
          {key:'Y',label:'必输'},
          {key:'N',label:'非必输'},
        ],
        ruleForm: {
          CHANNELTYPE: '', //渠道
          BUSINESSTYPE:'',//业务类型
          TRANSCODE:'',//交易码
          SCORETEMPLATEID:'',//模板编号
          ID:''//ID
        },
        rules: {
          CHANNELTYPE: [{
            required: true,
            message: '请选择渠道',
            trigger: 'change'
          }],
          SCORETEMPLATEID: [{
            required: true,
            message: '请输入模板编号',
            trigger: 'change'
          }]
          
        },
        loading: false,
        
      };
    },
    created() {

    },
    methods: {
      //选择数据库列的时候触发
      // changeDBCOL(){
      //   this.ruleForm.COLNAME = ''
      //   this.ruleForm.DATALEN = ''
      //   this.ruleForm.DATAMUST = ''
      //   this.ruleForm.COLNAME = this.findName(this.option.DBCOLOption)
      //   this.ruleForm.DATALEN = this.findName(this.option.lengthOption)
      //   this.ruleForm.DATAMUST = this.findName(this.option.inputOption)
      // },
      changeType(){
        this.queryRelation()
      },
      changeItem(){
        this.typeQuery()
      },
       //查询业务类型
      async typeQuery() {
        this.typeOption = []
        let resBody = new type();
        resBody.data.CHANNELTYPE = this.ruleForm.CHANNELTYPE
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          let arr  = response.BODY.RSPSTRUCT;
          if (this.ruleForm.CHANNELTYPE == '') {
            this.typeOption = this.unqiueArr(arr)
          }else{
            this.typeOption = arr
          }
          this.queryRelation()
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      // 查询交易码
      async queryRelation() {
        this.relationOption = []
        let resBody = new relation()
        resBody.data.CHANNELTYPE = this.ruleForm.CHANNELTYPE;
        resBody.data.BUSINESSTYPE = this.ruleForm.BUSINESSTYPE;
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.relationOption = response.BODY.RSPSTRUCT;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      findName(arr){
        let data = arr.filter(item=>{
          return item.FIELDKEY == this.ruleForm.DBCOL
        })
        return data[0].FIELDVALUE
      },
      openDialog() {
        this.relationOption = JSON.parse(JSON.stringify(this.option.relationOption));
        if(this.$refs['ruleForm'] != undefined){
          this.$refs['ruleForm'].clearValidate();
        }
        if (this.option.flag == 'add') {
          this.ruleForm = {
            CHANNELTYPE: '', //渠道类型
            BUSINESSTYPE: '', //业务类型
          }
        } else {
          this.ruleForm = JSON.parse(JSON.stringify(this.option.row));
          this.typeOption = this.option.typeOption
        }
      },
      closeDialog(formName) {
        if(this.$refs[formName] != undefined){
          this.$refs[formName].clearValidate();
        }
        // this.$refs[formName].clearValidate();
        this.$refs[formName].resetFields();
      },
      //保存
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            let resBody
            resBody = this.option.flag == 'add' ? new addData() : new updateData();
            resBody.data.BUSINESSTYPE = this.ruleForm.BUSINESSTYPE
            resBody.data.TRANSCODE = this.ruleForm.TRANSCODE
            resBody.data.CHANNELTYPE = this.ruleForm.CHANNELTYPE
            resBody.data.SCORETEMPLATEID = this.ruleForm.SCORETEMPLATEID
            if(this.option.flag == 'update'){
              resBody.data.ID = this.ruleForm.ID
            }
            
            
            let response = await request(resBody);
            if (response.SYS_HEAD.ReturnCode == "000000") {
              if (this.option.flag == 'update') {
                this.$message({
                  type: 'success',
                  message: '修改成功！'
                })
              } else {
                this.$message({
                  type: 'success',
                  message: '新增成功！'
                })
              }
              this.isShow = false;
              this.$emit("refreshTable");
            } else {
              this.$message({
                type: "error",
                message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD
                  .ReturnMessage
              })
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].clearValidate();
          this.$refs[formName].resetFields();
        }
      }
    }
  };

</script>

<style scoped>

</style>
